package com.admin.web;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.admin.service.AdminSchoolService;
import com.common.model.AdminUser;
import com.common.model.BaseUser;
import com.common.model.Department;
import com.common.model.ProfBranch;
import com.common.model.Profession;

@Controller
@RequestMapping("/admin/branch")
public class AdminBranchWeb {
	@Autowired
	AdminSchoolService service;
	/**
	 * 根据条件查询方向数据
	 * @param clgId 学院id
	 * @param depId 系id
	 * @param proId 专业id
	 * @param page 页码(从1开始)
	 * @return
	 */
	@RequestMapping("/showBranchList/{clg_id}/{dep_id}/{pro_id}/{page}")
	public ModelAndView showBranch(
			@PathVariable("clg_id") int clgId,
			@PathVariable("dep_id")int depId,
			@PathVariable("pro_id")int proId,
			@PathVariable("page")int page){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("admin/school/showBranch");
		mav.addObject("colleges", service.getAllCollege());
		mav.addObject("departments",service.getDepsOfCollege(clgId));
		mav.addObject("professions",service.getProsOfDeps(depId));
		mav.addObject("clgId", clgId);
		mav.addObject("depId", depId);
		mav.addObject("proId", proId);
		mav.addObject("branches", service.getBranchBy(clgId, depId, proId, page));
		int branchCount=service.getBranchCountBy(clgId, depId, proId);
		int totalPage=(int)Math.ceil(branchCount/10.0);
		if(totalPage==0){
			totalPage=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		mav.addObject("page", page);
		mav.addObject("branchesCount",branchCount );
		mav.addObject("totalPage",totalPage);
		mav.addObject("currentMenu", "showBranchList");
		return mav;
	}
	/**
	 * 获取某学院下的所有系
	 * @param collegeId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getDepsOfCollege/{college_id}")
	public ArrayList<Department>getDepsOfCollege(@PathVariable("college_id") int collegeId) {
		return service.getDepsOfCollege(collegeId);
	}
	
	/**
	 * 读取某系下的所有专业
	 * @param depId 系id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getProsOfDeps/{dep_id}")
	public ArrayList<Profession>getProsOfDeps(@PathVariable("dep_id")int depId){
		return service.getProsOfDeps(depId);
	}
	/**
	 * 读取某专业下的所有专业方向
	 * @param proId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getBranchesOfPro/{pro_id}")
	public ArrayList<ProfBranch>getBranchesOfPro(@PathVariable("pro_id")int proId){
		return service.getBranchesOfPro(proId);
	}
	/**
	 * 根据专业方向id查询专业方向
	 * @param pbId 专业方向id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getProBranch/{pb_id}")
	public ProfBranch getProBranchByPbId(@PathVariable("pb_id")int pbId){
		return service.getProBranchByPbId(pbId);
	}
	/**
	 * 修改专业方向信息
	 * @param pb
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateProBranch")
	public int updateProBranch(ProfBranch pb,HttpSession session){
		if(pb.getId()==null||pb.getName()==null||pb.getName().trim().isEmpty()||pb.getNo()==null||pb.getProfId()==null){
			return -3;//参数错误
		}
		AdminUser admin=(AdminUser) session.getAttribute("user");
		//检查正在操作的用户是否为超级管理员
		if(!admin.isSuperAdmin()){
			//如果不是超级管理员,检查正在操作的专业方向是否为本学院的
			int orgCollegeId=service.getProBranchByPbId(pb.getId()).getProf().getDepartment().getCollege().getId();
			if(admin.getCollegeId()!=orgCollegeId){
				return -1;//操作之前不是本学院的
			}
			int newCollegeId=service.getProfessionByProId(pb.getProfId()).getDepartment().getCollege().getId();
			if(admin.getCollegeId()!=newCollegeId){
				return -2;//操作之后不是本学院的
			}
		}

		ProfBranch pbCheck=service.getBranchByBranchNo(pb.getNo());
		if(pbCheck!=null&&pbCheck.getId()!=pb.getId()){
			return -4;//重复no;
		}
		pb.setName(pb.getName().trim());
		try {
			service.updateProBranch(pb);
		} catch (Exception e) {
			return -5;//参数错误2
		}
		return 0;
	}
	
	/**
	 * 删除某个专业方向
	 * @param pbId
	 * @return -1专业方向之下还有班级存留,-2 专业方向之下还有题目存留 ,-3 无权操作,0成功
	 */
	@ResponseBody
	@RequestMapping("/deleteBranch/{pb_id}")
	public int deleteProBranch(@PathVariable("pb_id")int pbId,HttpSession session){
		AdminUser admin=(AdminUser) session.getAttribute("user");
		//检查正在操作的用户是否为超级管理员
		if(!admin.isSuperAdmin()){
			//如果不是超级管理员,检查正在操作的专业方向是否为本学院的
			int orgCollegeId=service.getProBranchByPbId(pbId).getProf().getDepartment().getCollege().getId();
			if(admin.getCollegeId()!=orgCollegeId){
				return -3;//无权操作
			}
		}
		return service.deleteProBranch(pbId);
	}
	/**
	 * 添加一个专业方向
	 * @param pb
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/insertProBranch")
	public int insertProBranch(ProfBranch pb,HttpSession session){
		if(pb.getName()==null||pb.getName().trim().isEmpty()||pb.getNo()==null||pb.getProfId()==null){
			return -2;//参数错误
		}
		AdminUser admin=(AdminUser) session.getAttribute("user");
		//检查正在操作的用户是否为超级管理员
		if(!admin.isSuperAdmin()){
			//如果不是超级管理员,检查添加的专业方向是否为本学院的
			int newCollegeId=service.getProfessionByProId(pb.getProfId()).getDepartment().getCollege().getId();
			if(admin.getCollegeId()!=newCollegeId){
				return -1;//添加的专业方向不属于本学院
			}
		}
		pb.setName(pb.getName().trim());
		int result=service.insertProBranch(pb);
		if(result==-1){
			return -3;//已存在同样no的专业方向
		}
		if(result==-2){
			return -4;//参数错误2
		}
		return 0;
	}
	/**
	 * 获取某系下的所有专业方向
	 * @param depId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getAllBranchOfDep/{dep_id}")
	public ArrayList<ProfBranch> getAllBranchOfDepartment(@PathVariable("dep_id")int depId){
		return service.getAllBranchOfDepartment(depId);
	}
}
